<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Python 的变量 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Python 的变量</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-的变量"><a href="#Python-的变量" class="headerlink" title="Python 的变量"></a>Python 的变量</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;变量是计算机内存中的一块区域，变量可以存储规定范围内的值，而且值可以改变。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;python下变量是对一个数据的引用。</p>
<h2 id="变量的命名："><a href="#变量的命名：" class="headerlink" title="变量的命名："></a>变量的命名：</h2><ol>
<li>变量名由字母、数字、下划线组成。</li>
<li>变量不能以数字开头</li>
<li>不可以使用关键字</li>
<li>a a1 _a，这些都是合法的变量名<br> 表达式是将不同的数据（包括变量、函数）用运算符号按一定规则连接起来的一种式子。</li>
</ol>
<h3 id="Python对象的三要素：id、type、value"><a href="#Python对象的三要素：id、type、value" class="headerlink" title="Python对象的三要素：id、type、value"></a>Python对象的三要素：id、type、value</h3><ul>
<li>id：是对象的唯一标识，是对象在内存中的存储地址</li>
<li>type：是对象的数据类型</li>
<li>value：是对象的值，是对象在内存中存放的数据</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有一个name，理解为变量名</p>
<p>###变量的赋值：<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;变量的声明和定义的过程</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">1</span>]: a=<span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">2</span>]: id(a)</div><div class="line">Out[<span class="number">2</span>]: <span class="number">13933672</span></div><div class="line"> </div><div class="line">In [<span class="number">3</span>]: id(a)</div><div class="line">Out[<span class="number">3</span>]: <span class="number">13933672</span></div><div class="line"> </div><div class="line">In [<span class="number">4</span>]: a=<span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">5</span>]: id(a)</div><div class="line">Out[<span class="number">5</span>]: <span class="number">13933648</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;id（a）表示变量a在内存里的位置，a的变量值发生改变，在内存中的位置也发生改变。</p>
<h2 id="Python-运算符"><a href="#Python-运算符" class="headerlink" title="Python 运算符"></a>Python 运算符</h2><h3 id="1-算数运算符"><a href="#1-算数运算符" class="headerlink" title="1.算数运算符"></a>1.算数运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;+，一个值加上另一个值，也可以是一个字符串加上另一个字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">38</span>]: <span class="number">3</span> + <span class="number">4</span></div><div class="line">Out[<span class="number">38</span>]: <span class="number">7</span></div><div class="line"> </div><div class="line">In [<span class="number">39</span>]: <span class="string">'a'</span> + <span class="string">'b'</span></div><div class="line">Out[<span class="number">39</span>]: <span class="string">'ab'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-，一个值减去另一个值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">40</span>]: <span class="number">5</span> - <span class="number">4</span></div><div class="line">Out[<span class="number">40</span>]: <span class="number">1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;*，一个值乘以另一个值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">44</span>]: <span class="number">3</span> * <span class="number">4</span></div><div class="line">Out[<span class="number">44</span>]: <span class="number">12</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/，一个值除以另一个值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">45</span>]: <span class="number">4</span> / <span class="number">3</span></div><div class="line">Out[<span class="number">45</span>]: <span class="number">1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：这里只取整数，如果想要小数点，那么除法运算的两个数值至少一个要是浮点数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">46</span>]: <span class="number">4.0</span> / <span class="number">3</span></div><div class="line">Out[<span class="number">46</span>]: <span class="number">1.3333333333333333</span></div><div class="line"> </div><div class="line">In [<span class="number">47</span>]: <span class="number">4</span> / <span class="number">3.0</span></div><div class="line">Out[<span class="number">47</span>]: <span class="number">1.3333333333333333</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//，只取整除，数值带上小数点也只取整数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">48</span>]: <span class="number">4.0</span> // <span class="number">3</span></div><div class="line">Out[<span class="number">48</span>]: <span class="number">1.0</span></div><div class="line"> </div><div class="line">In [<span class="number">49</span>]: <span class="number">4</span> // <span class="number">3.0</span></div><div class="line">Out[<span class="number">49</span>]: <span class="number">1.0</span></div><div class="line"> </div><div class="line">In [<span class="number">50</span>]: <span class="number">4</span> // <span class="number">3</span></div><div class="line">Out[<span class="number">50</span>]: <span class="number">1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;%，表示取余</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">51</span>]: <span class="number">4</span> % <span class="number">3</span></div><div class="line">Out[<span class="number">51</span>]: <span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">52</span>]: <span class="number">5</span> % <span class="number">3</span></div><div class="line">Out[<span class="number">52</span>]: <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">53</span>]: <span class="number">6</span> % <span class="number">3</span></div><div class="line">Out[<span class="number">53</span>]: <span class="number">0</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;**，表示乘方</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">54</span>]: <span class="number">2</span> ** <span class="number">3</span></div><div class="line">Out[<span class="number">54</span>]: <span class="number">8</span></div><div class="line"> </div><div class="line">In [<span class="number">55</span>]: <span class="number">2</span> ** <span class="number">10</span></div><div class="line">Out[<span class="number">55</span>]: <span class="number">1024</span></div></pre></td></tr></table></figure>
<h3 id="2-关系（比较）运算符"><a href="#2-关系（比较）运算符" class="headerlink" title="2.关系（比较）运算符"></a>2.关系（比较）运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;，大于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">56</span>]: <span class="number">2</span> &gt; <span class="number">1</span></div><div class="line">Out[<span class="number">56</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">57</span>]: <span class="number">1</span> &gt; <span class="number">2</span></div><div class="line">Out[<span class="number">57</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;，小于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">58</span>]: <span class="number">2</span> &lt; <span class="number">3</span></div><div class="line">Out[<span class="number">58</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">59</span>]: <span class="number">3</span> &lt; <span class="number">2</span></div><div class="line">Out[<span class="number">59</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;=，大于等于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">60</span>]: <span class="number">2</span> &gt;= <span class="number">1</span></div><div class="line">Out[<span class="number">60</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">61</span>]: <span class="number">2</span> &gt;= <span class="number">2</span></div><div class="line">Out[<span class="number">61</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">62</span>]: <span class="number">2</span> &gt;= <span class="number">3</span></div><div class="line">Out[<span class="number">62</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;=，小于等于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">63</span>]: <span class="number">2</span> &lt;=<span class="number">3</span></div><div class="line">Out[<span class="number">63</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">64</span>]: <span class="number">3</span> &lt;= <span class="number">3</span></div><div class="line">Out[<span class="number">64</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">65</span>]: <span class="number">4</span> &lt;= <span class="number">3</span></div><div class="line">Out[<span class="number">65</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;==，恒等于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">72</span>]: <span class="number">1</span> == <span class="number">1</span></div><div class="line">Out[<span class="number">72</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">73</span>]: <span class="number">1</span> == <span class="number">2</span></div><div class="line">Out[<span class="number">73</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;!=，不等于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">74</span>]: <span class="number">1</span> != <span class="number">2</span></div><div class="line">Out[<span class="number">74</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">75</span>]: <span class="number">2</span> != <span class="number">2</span></div><div class="line">Out[<span class="number">75</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<h3 id="3-赋值运算符"><a href="#3-赋值运算符" class="headerlink" title="3.赋值运算符"></a>3.赋值运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;=，表示直接给变量赋值，如果是字符串必须加上引号</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">11</span>]: x = <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">12</span>]: y = <span class="string">'abc'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：可以通过 type（）命令查看变量是数字还是字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">15</span>]: type(x)</div><div class="line">Out[<span class="number">15</span>]: int</div><div class="line"> </div><div class="line">In [<span class="number">16</span>]: type(y)</div><div class="line">Out[<span class="number">16</span>]: str</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;+=，表示变量 的值再加上一个值（a=a+b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">13</span>]: x += <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">14</span>]: x</div><div class="line">Out[<span class="number">14</span>]: <span class="number">5</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-=，表示变量的值再减去一个值（a=a-b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">17</span>]: x -= <span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">18</span>]: x</div><div class="line">Out[<span class="number">18</span>]: <span class="number">4</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;*=，表示变量的值再乘以一个值（a=a*b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">19</span>]: x *= <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">20</span>]: x</div><div class="line">Out[<span class="number">20</span>]: <span class="number">8</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/=，表示变量的值再除以一个值（a=a/b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">21</span>]: x /= <span class="number">4</span></div><div class="line"> </div><div class="line">In [<span class="number">22</span>]: x</div><div class="line">Out[<span class="number">22</span>]: <span class="number">2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;%=，表示取余，变量被一个数整除后，还剩多少值（a=a%b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">29</span>]: x=<span class="number">10</span></div><div class="line"> </div><div class="line">In [<span class="number">30</span>]: x %= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">31</span>]: x</div><div class="line">Out[<span class="number">31</span>]: <span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">32</span>]: x=<span class="number">9</span></div><div class="line"> </div><div class="line">In [<span class="number">33</span>]: x %= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">34</span>]: x</div><div class="line">Out[<span class="number">34</span>]: <span class="number">0</span></div><div class="line"> </div><div class="line">In [<span class="number">35</span>]: x=<span class="number">11</span></div><div class="line"> </div><div class="line">In [<span class="number">36</span>]: x %= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">37</span>]: x</div><div class="line">Out[<span class="number">37</span>]: <span class="number">2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;**=，表示乘方，a的b次方（a=a**b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">66</span>]: x = <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">67</span>]: x **= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">68</span>]: x</div><div class="line">Out[<span class="number">68</span>]: <span class="number">8</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//=，表示整除，（a=a//b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">68</span>]: x</div><div class="line">Out[<span class="number">68</span>]: <span class="number">8</span></div><div class="line"> </div><div class="line">In [<span class="number">69</span>]: x //= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">70</span>]: x</div><div class="line">Out[<span class="number">70</span>]: <span class="number">2</span></div></pre></td></tr></table></figure>
<h3 id="4-逻辑运算符"><a href="#4-逻辑运算符" class="headerlink" title="4.逻辑运算符"></a>4.逻辑运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;and，逻辑与：true and false。表示一个成立and另一个也成立，返回值是 true</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">77</span>]: <span class="number">3</span> &gt; <span class="number">2</span> <span class="keyword">and</span> <span class="number">1</span> &lt; <span class="number">2</span></div><div class="line">Out[<span class="number">77</span>]: <span class="keyword">True</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果其中一个不成立，返回值是 false</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">78</span>]: <span class="number">1</span> == <span class="number">2</span> <span class="keyword">and</span> <span class="number">2</span> &gt; <span class="number">1</span></div><div class="line">Out[<span class="number">78</span>]: <span class="keyword">False</span></div><div class="line"> </div><div class="line">In [<span class="number">79</span>]: <span class="number">1</span> &lt; <span class="number">2</span> <span class="keyword">and</span> <span class="number">1</span> != <span class="number">1</span></div><div class="line">Out[<span class="number">79</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;or，逻辑或：false or true，表示只要有一个成立那么返回就是true</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">80</span>]: <span class="number">1</span> &lt; <span class="number">2</span> <span class="keyword">or</span> <span class="number">1</span> != <span class="number">1</span></div><div class="line">Out[<span class="number">80</span>]: <span class="keyword">True</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;not，逻辑非：not true，表示取反</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">81</span>]: <span class="keyword">not</span> <span class="number">1</span> &lt; <span class="number">2</span></div><div class="line">Out[<span class="number">81</span>]: <span class="keyword">False</span></div><div class="line"> </div><div class="line">In [<span class="number">82</span>]: <span class="keyword">not</span> <span class="number">1</span> != <span class="number">1</span></div><div class="line">Out[<span class="number">82</span>]: <span class="keyword">True</span></div></pre></td></tr></table></figure>
<h3 id="5-身份运算符"><a href="#5-身份运算符" class="headerlink" title="5.身份运算符"></a>5.身份运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;身份运算符用来判断两个变量的引用对象是否指向统一个内存的对象，即 id(varibale1)?=id(variable2)。</p>
<ul>
<li>is：判断两个标识符是不是引用自一个对象</li>
<li>is not：判断两个标识符是不是引用不同对象</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">88</span>]: a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</div><div class="line"> </div><div class="line">In [<span class="number">89</span>]: b = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</div><div class="line"> </div><div class="line">In [<span class="number">90</span>]: <span class="keyword">print</span> ( a == b )</div><div class="line"><span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">91</span>]: <span class="keyword">print</span> ( a <span class="keyword">is</span> b )</div><div class="line"><span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为变量 a 和变量 b 的 value 是一样的， == 运算符比较的变量的 value ，所以返回true。is 是判断两个变量是否引用同一个对象，也就是比较的是 id ，所以返回 false。</p>
<h3 id="6-成员关系运算符"><a href="#6-成员关系运算符" class="headerlink" title="6.成员关系运算符"></a>6.成员关系运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;成员运算符能够判断一个指定对象是否是作为一个容器中的元素，由此来判断两个对象间的关系。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;容器：包含了其他对象的引用的数据类型。</p>
<ul>
<li>in：当一个对象存在一个容器中时为 true</li>
<li>not in：当一个对象不在一个容器中时为 true</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">93</span>]: a = <span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">94</span>]: b = <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">95</span>]: c = [<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>]</div><div class="line"> </div><div class="line">In [<span class="number">96</span>]: a <span class="keyword">in</span> c</div><div class="line">Out[<span class="number">98</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">97</span>]: b <span class="keyword">in</span> c</div><div class="line">Out[<span class="number">99</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<h3 id="7-位运算符"><a href="#7-位运算符" class="headerlink" title="7.位运算符"></a>7.位运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 Python 中 |、&amp;、^、~等运算符的意思不在于处理逻辑关系，而是二进制数据的为运算，数字以二进制形式的补码存储和计算，以原码结果来显示。若数字为正值，它的补码就是原码本身、若为负值，则它的补码为源码减1再按位取反。两个数字的计算本质是两个二进制补码的计算。</p>
<ul>
<li>|：按位或运算符，只要对应的二个二进位有一个为1时，结果位就为1.</li>
<li>&amp;：按位与运算符，参与运算的两个值，如果两个相应的位都为1，则该位为的结果为1，否则为0</li>
<li>^：按位异或运算符，当两对应的二进位相异时，结果为1</li>
<li>~：按位取反运算符，对数据的每个二进制位取反，即把1变为0，把0变为1</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">a = <span class="number">0011</span> <span class="number">1100</span></div><div class="line"> </div><div class="line">b = <span class="number">0000</span> <span class="number">1101</span></div><div class="line"> </div><div class="line">-----------------</div><div class="line"> </div><div class="line">a&amp;b = <span class="number">0000</span> <span class="number">1100</span></div><div class="line"> </div><div class="line">a|b = <span class="number">0011</span> <span class="number">1101</span></div><div class="line"> </div><div class="line">a^b = <span class="number">0011</span> <span class="number">0001</span></div><div class="line"> </div><div class="line">~a = <span class="number">1100</span> <span class="number">0011</span></div></pre></td></tr></table></figure>
<h3 id="8-位移运算符"><a href="#8-位移运算符" class="headerlink" title="8.位移运算符"></a>8.位移运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;位移运算符是非常有效率的计算方法之一，在对数学运算和对程序执行效率要求高的程序中推荐使用。</p>
<ul>
<li>&lt;&lt;：左移动运算符，运算数的各二进位全部左移若干位，由 “&lt;&lt;” 右边的数值指定移动的位数，高位丢弃，低位补0</li>
<li><blockquote>
<blockquote>
<p>：右移动运算符，把 “&gt;&gt;” 左边的运算数的各二进位全部右移若干位， “&gt;&gt;” 右边的数指定移动的位数</p>
</blockquote>
</blockquote>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">a = <span class="number">0011</span> <span class="number">1100</span></div><div class="line"> </div><div class="line">a &lt;&lt; <span class="number">2</span> <span class="number">1111</span> <span class="number">0000</span> <span class="comment">#结果为240</span></div><div class="line"> </div><div class="line">a &gt;&gt; <span class="number">2</span> <span class="number">0000</span> <span class="number">1111</span> <span class="comment">#结果为15</span></div></pre></td></tr></table></figure>
<h2 id="Python运算符优先级"><a href="#Python运算符优先级" class="headerlink" title="Python运算符优先级"></a>Python运算符优先级</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下标列出了从最高到最低优先级的所有运算符：</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>**</td>
<td>指数 (最高优先级)</td>
</tr>
<tr>
<td>~ + -</td>
<td>按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)</td>
</tr>
<tr>
<td>* / % //</td>
<td>乘，除，取模和取整除</td>
</tr>
<tr>
<td>+ -</td>
<td>加法减法</td>
</tr>
<tr>
<td>&gt;&gt; &lt;&lt;</td>
<td>右移，左移运算符</td>
</tr>
<tr>
<td>&amp;</td>
<td>位 ‘AND’</td>
</tr>
<tr>
<td>^ \</td>
<td></td>
<td>位运算符</td>
</tr>
<tr>
<td>&lt;= &lt; &gt; &gt;=</td>
<td>比较运算符</td>
</tr>
<tr>
<td>&lt;&gt; == !=</td>
<td>等于运算符</td>
</tr>
<tr>
<td>= %= /= //= -= += <em>= *</em>=</td>
<td>赋值运算符</td>
</tr>
<tr>
<td>is is not</td>
<td>身份运算符</td>
</tr>
<tr>
<td>in not in</td>
<td>成员运算符</td>
</tr>
<tr>
<td>not or and</td>
<td>逻辑运算符</td>
</tr>
</tbody>
</table>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例演示了python所有运算符优先级的操作：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line">  </div><div class="line">a = <span class="number">20</span></div><div class="line">b = <span class="number">10</span></div><div class="line">c = <span class="number">15</span></div><div class="line">d = <span class="number">5</span></div><div class="line">e = <span class="number">0</span></div><div class="line">  </div><div class="line">e = (a + b) * c / d <span class="comment">#( 30 * 15 ) / 5</span></div><div class="line"><span class="keyword">print</span> <span class="string">"(a + b) * c / d 运算结果为："</span>, e</div><div class="line">  </div><div class="line">e = ((a + b) * c) / d <span class="comment"># (30 * 15 ) / 5</span></div><div class="line"><span class="keyword">print</span> <span class="string">"((a + b) * c) / d 运算结果为："</span>, e</div><div class="line">  </div><div class="line">e = (a + b) * (c / d); <span class="comment"># (30) * (15/5)</span></div><div class="line"><span class="keyword">print</span> <span class="string">"(a + b) * (c / d) 运算结果为："</span>, e</div><div class="line">  </div><div class="line">e = a + (b * c) / d; <span class="comment"># 20 + (150/5)</span></div><div class="line"><span class="keyword">print</span> <span class="string">"a + (b * c) / d 运算结果为："</span>, e</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">(a + b) * c / d 运算结果为： <span class="number">90</span></div><div class="line">((a + b) * c) / d 运算结果为： <span class="number">90</span></div><div class="line">(a + b) * (c / d) 运算结果为： <span class="number">90</span></div><div class="line">a + (b * c) / d 运算结果为： <span class="number">50</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.4 Python 的变量/5. Python 的变量/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
